<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入名称"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="别名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['alias', validatorRules.alias]" placeholder="请输入别名"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['regulation', validatorRules.regulation]" placeholder="请输入规则"  ></a-input>
              <a-collapse>
                <a-collapse-panel key="1" header="字段说明">
              <p>{yyyy}{MM}{dd}{NO}</p>
              <p> {yyyy}表示年份</p>
              <p> {MM}表示月份，如果月份小于10，则加零补齐，如1月份表示为01。</p>
              <p>{mm}表示月份，月份不补齐，如1月份表示为1。</p>
              <p>{DD}表示日，如果小于10号，则加零补齐，如1号表示为01。</p>
              <p>{dd}表示日，日期不补齐，如1号表示为1。</p>
              <p> {NO}表示流水号，前面补零。</p>
              <p>{no}表示流水号，后面补零。</p>
                </a-collapse-panel>
              </a-collapse>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="生成类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag v-decorator="['gentype', validatorRules.gentype]" placeholder="请输入生成类型"
                                 style="width: 100%" :triggerChange="true" dictCode="GEN_TYPE"
                                 type="radio"></j-dict-select-tag>
              <a-collapse>
                <a-collapse-panel key="1" header="字段说明">
              <p>每天生成:每天从初始值开始计数。</p>
              <p> 每月生成:每月从初始值开始计数。</p>
              <p> 每年生成:每年从初始值开始计数。</p>
              <p> 递增:流水号一直增加。</p>
                </a-collapse-panel>
              </a-collapse>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="流水号长度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['nolength', validatorRules.nolength]" placeholder="请输入流水号长度" style="width: 100%" />
              <a-collapse>
                <a-collapse-panel key="1" header="字段说明">
              <p>这个长度表示当前流水号的长度数，只包括流水号部分{NO},如果长度为5，当前流水号为5，则在流水号前补4个0，表示为00005。</p>
              <p>{no}如果长度为5，当前流水号为501，则在流水号后面补5个0，表示为50100000，如果长度为1，则流水号一直递增</p>
                </a-collapse-panel>
              </a-collapse>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="初始值" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['initvalue', validatorRules.initvalue]" placeholder="请输入初始值" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="步长" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['step', validatorRules.step]" placeholder="请输入步长" style="width: 100%" />
              <a-collapse>
                <a-collapse-panel key="1" header="字段说明">
              <p>流水号每次递加的数字，默认步长为1。比如步长为2，则每次获取流水号则在原来的基础上加2。</p>
                </a-collapse-panel>
              </a-collapse>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/ksource/JFormContainer'

  export default {
    name: 'SysIdentityForm',
    components: {
      JFormContainer,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: {
            rules: [
              { required: true, message: '请输入名称!'},
            ]
          },
          alias: {
            rules: [
              { required: true, message: '请输入别名!'},
            ]
          },
          regulation: {
            rules: [
              { required: true, message: '请输入规则!'},
            ]
          },
          gentype: {
            rules: [
              { required: true, message: '请输入生成类型!'},
            ]
          },
          nolength: {
            rules: [
              { required: true, message: '请输入流水号长度!'},
            ]
          },
          initvalue: {
            rules: [
              { required: true, message: '请输入初始值!'},
            ]
          },
          step: {
            rules: [
              { required: true, message: '请输入步长!'},
            ]
          },
        },
        url: {
          add: "/system/sysIdentity/add",
          edit: "/system/sysIdentity/edit",
          queryById: "/system/sysIdentity/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','alias','regulation','gentype','nolength','initvalue','step'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','alias','regulation','gentype','nolength','initvalue','step'))
      },
    }
  }
</script>
